const sql = require("../../mysql");

module.exports = async ctx => {
  try {
    const { idArray } = ctx.request.body;

    async function deleteFiles(array) {
      await sql.Vfile.destroy({
        where: {
          id: {
            [sql.Op.in]: array
          },
          userId: ctx.request.userId
        }
      });

      let files = await sql.Vfile.findAll({
        where: {
          parentId: {
            [sql.Op.in]: array
          }
        }
      });

      let newArray = files.map(e => e.id);

      if (newArray.length) {
        await deleteFiles(newArray);
      }
    }

    await deleteFiles(idArray);

    ctx.body = {
      code: 0,
      msg: "删除成功！"
    };
  } catch (error) {
    console.log(error);
    ctx.response.status = 200;

    ctx.body = {
      code: -1,
      msg: error.errors ? error : error.errors[0].message
    };
  }
};
